package com.bee.test;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;

public class SSEClient {

    public static void main(String[] args) {
        HttpRequest request = HttpRequest.get("http://www.baidu.com")
                .header("Accept", "text/event-stream");

        HttpResponse response = request.executeAsync();

        // Process the SSE stream
        processSSEStream(response.body());
    }

    private static void processSSEStream(String responseBody) {
        String[] lines = responseBody.split("\\r?\\n");

        for (String line : lines) {
            if (line.isEmpty()) {
                // End of event, process the event received
                processEvent();
            } else {
                // Process the line, it could be event data or a comment
                System.out.println("Received line: " + line);
            }
        }
    }

    private static void processEvent() {
        // Implement your event processing logic here
        System.out.println("Received event!");
    }
}
